package thread;

import java.util.Random;

/**
 * Created with IDEA
 * author:ju bai
 * Date:2021/4/27
 * Time:10:58
 * 线程分组-百米赛跑
 **/
public class ThreadDemo6 {
    public static void main(String[] args) {
        //定义任务
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("开始起跑"+Thread.currentThread().getName());
                int num = 1+new Random().nextInt(5);
                try {
                    Thread.sleep(num*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("到终点"+Thread.currentThread().getName());
            }
        };
        //定义分组
        ThreadGroup group = new ThreadGroup("百米赛跑一组");
        //创建运动员
        Thread t1 = new Thread(group,runnable,"qwe");
        Thread t2 = new Thread(group,runnable,"asd");
        t1.start();
        t2.start();
        //打印分组情况
        group.list();
        //等待所有选手到达终点
        while(group.activeCount()!=0){
        }
        //宣布成绩
        System.out.println("宣布成绩");
    }
}
